package com.mobile.check.present;

import cn.droidlover.xdroidmvp.cache.SharedPref;
import cn.droidlover.xdroidmvp.net.XApi;
import io.reactivex.functions.Consumer;
import com.mobile.check.activitys.LoginActivity;
import com.mobile.check.beans.TokenInfo;
import com.mobile.check.net.Api;
import com.mobile.check.net.BaseObjBean;
import com.mobile.check.util.AppContents;

public class LoginPresent extends BasePresent<LoginActivity> {
    public void login(String idCard, String pwd) {
        Api.getApiService().login(idCard, pwd)
                .compose(XApi.<BaseObjBean<TokenInfo>>getApiTransformer())
                .compose(XApi.<BaseObjBean<TokenInfo>>getScheduler())
                .compose(getV().<BaseObjBean<TokenInfo>>bindToLifecycle())
                .subscribe(new Consumer<BaseObjBean<TokenInfo>>() {
                    @Override
                    public void accept(BaseObjBean<TokenInfo> objBean) throws Exception {
                        SharedPref.getInstance(getV()).putString(AppContents.TOKEN, objBean.getData().getToken());
                        getV().loginSuccess();
                    }
                }, failConsumer);

    }
}
